import random
# 定义一个列表，用来存储所有的用户的信息（每个用户是一个字典）
info_list = [{'id':'00000001', 'name':'小明', 'password':'123456', 'money':1000}, {'id':'00000002', 'name':'小红', 'password':'123456', 'money':1100}, {'id':'00000003', 'name':'小刚', 'password':'123456', 'money':2000}]
def print_menu():
    print('************************')
    print('*   中国农业银行账号管理系统        *')
    print('************************')
    print('*   选项        *')
    print('*   1.开户        *')
    print('*   2.存钱        *')
    print('*   3.取钱        *')
    print('*   4.转账        *')
    print('*   5.查询        *')
    print('*   6.Bye        *')
def add_user(): #开户功能
    #生成一个随机的用户的账号
    userid = str(random.randint(10000000, 99999999))

    username = input('请输入你的姓名:')
    userpassword = input('请输入你的密码:')
    #useraddress = input('请输入你的地址:')
    usermoney = int(input('请输入你的钱数:'))
    for temp_info in info_list:
        if temp_info['id'] == userid:
            print('用户账号已存在')
            return 2
    info = dict()
    info['id'] = userid
    info['name'] = username
    info['password'] = userpassword
    info['money'] = usermoney
    info_list.append(info)
    #print(info_list)
    if len(info_list) <= 100:
        print(info_list)
        print('1.用户账号创建成功')
        return 1
    else:
        print('用户库已注册满')
        return 3
def add_money():
    userid = input('请输入你的用户账号')
    for temp_info in info_list:
        if temp_info['id'] == userid:
            addmoney = int(input('请输入你要存取的钱数:'))
            for idx, d in enumerate(info_list):
                if d.get("id") == userid:
                        print(idx)
                        info_list[idx]['money'] += addmoney
                        break
        else:
            print('用户账号不存在无法充值')
def del_money():
    userid = input('请输入你的用户账号')
    for temp_info in info_list:
        if temp_info['id'] == userid:
            print('0：正常')
            userpassword = input('请输入你的密码:')
            for temp_info in info_list:
                if temp_info['password'] == userpassword and temp_info['id'] == userid:
                    delmoney = int(input('请输入你要取走的钱数'))
                    for idx, d in enumerate(info_list):
                        if d.get("id") == userid:
                            print(idx)
                            if info_list[idx]['money'] >= delmoney:
                                info_list[idx]['money'] -= delmoney
                                break
                            else:
                                print('3：钱不够')
                else:
                    print('2：密码不对')

        else:
            print('1：账号不存在')
def change_money():
    userid = input('请输入你的用户账号')
    userid1 = input('请输入你要给别人转账的用户账号')
    for temp_info in info_list:
        if temp_info['id'] == userid:
            if temp_info['id'] == userid1:
                userpassword = input('请输入你的密码:')
                for temp_info in info_list:
                    if temp_info['password'] == userpassword and temp_info['id'] == userid:
                        delmoney = int(input('请输入你要转账的钱数'))
                        for idx, d in enumerate(info_list):
                            if d.get("id") == userid:
                                print(idx)
                                if info_list[idx]['money'] >= delmoney:
                                    info_list[idx]['money'] -= delmoney
                                    for idx, d in enumerate(info_list):
                                        if d.get("id") == userid1:
                                            print(idx)
                                            info_list[idx]['money'] += delmoney
                                            print('0：正常')
                                            break
                                else:
                                    print('3：你的余额不够')
                    else:
                        print('2：密码不正确')
            else:
                print('1：用户账号不存在')
        else:
            print('1：用户账号不存在')

def search():
    search_name = input("请输入要查询的用户账号:")
    for temp_info in info_list:
        if temp_info['id'] == search_name:
            userpassword = input('请输入你的密码:')
            if temp_info['password'] == userpassword and temp_info['id'] == search_name:
                print("查询到的信息如下:")
                print("用户账号:%s, 用户姓名:%s, 用户密码:%s, 用户余额:%s" % (temp_info['id'],temp_info['name'], temp_info['password'], temp_info['money']))
                break
            else:
                print('密码不正确')

        else:
            print('该用户不存在')
def main():
    while True:
        # 1. 打印功能
        print_menu()
        # 2. 获取用户的选择
        num = input('请输入要进行的操作(数字): ')
        # 3. 根据用户的选择，做相应的事情
        if num == '1':
            # 开户
            add_user()
        elif num == '2':
            # 存钱
            add_money()
        elif num == '3':
            # 取钱
            del_money()
        elif num == '4':
            # 转账
            change_money()
        elif num == '5':
            # 查询
            search()
        elif num == '6':
            break
        else:
            print("输入有误,请重新输入......")
main()
